package com.soonfor.evaluate.tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateTool {
    /*
     * 毫秒转化时分秒毫秒
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        }
        if (minute > 0) {
            sb.append(minute + "'");
        }
        if (second > 0) {
            sb.append(second + "''");
        }
        return sb.toString();
    }
    //毫秒转秒
    public static String long2String(long time) {

        //毫秒转秒
        int sec = (int) time / 1000;
        int min = sec / 60;    //分钟
        sec = sec % 60;        //秒
        if (min < 10) {    //分钟补0
            if (sec < 10) {    //秒补0
                return "0" + min + ":0" + sec;
            } else {
                return "0" + min + ":" + sec;
            }
        } else {
            if (sec < 10) {    //秒补0
                return min + ":0" + sec;
            } else {
                return min + ":" + sec;
            }
        }
    }
    /**
     * 输入时间字符串，返回想要的时间格式类型时间
     *
     * @param time 时间戳
     * @param type 时间格式
     * @return
     */
    public static String getTimeTimestamp(String time, String type) {
        if(time==null || time.equals("")){
            return "";
        }
        if(type==null)type = "yyyy-MM-dd HH:mm";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        try {
            long lt = new Long(time);
            Date date = new Date(lt);
            return simpleDateFormat.format(date);
        }catch (Exception e){
            if(time.contains(".")){
                return time.substring(0, time.indexOf("."));
            }
            return time;
        }
    }
    /**
     * 输入时间戳，返回想要的时间格式类型时间
     *
     * @param time 时间戳
     * @param type 时间格式
     * @return
     */
    public static String getTimeTimestamp(long time, String type) {
        if(time==0){
            return "";
        }
        if(type==null){
            SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日 HH:mm", Locale.getDefault());
            return sdr.format(new Date(time));
        }else {
            SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.getDefault());
            return sdr.format(new Date(time));
        }
    }
}
